//
//  Setting.m
//  reSearchDemo
//
//  Created by 云库 on 15/7/20.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "Setting.h"
#import "BaseTableView.h"
#import "UpdateManager.h"
#import "BaseTableViewCell.h"
#import "CameraActionSheet.h"
#import "AppDelegate.h"
#import "FriendPhotosViewController.h"
#import "Globals.h"
#import "CloudPlatformViewController.h"
#import "GAWebViewController.h"
@interface Setting ()
@property (nonatomic, strong) UpdateManager * updateManager;

@end

@implementation Setting
@synthesize updateManager;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    BaseTableView * table=[[BaseTableView alloc]init];
    table.frame=[[UIScreen mainScreen]bounds];
    //隐藏cell自带的分割线
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.navigationItem.title=@"设置";
    table.rowHeight=53;
    table.delegate=self;
    table.dataSource=self;
    [self.view addSubview:table];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)sender {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }
    else if (section == 1) {
        return 2;
    } else if (section == 2) {
        return 3;
    }
    return 3;
}
#pragma mark - UITableViewDataSource

- (UITableViewCell*)tableView:(UITableView *)sender cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* CellIdentifier = @"tableCell";
    BaseTableViewCell* cell = [sender dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    [cell addArrowRight];
    [cell setBottomLine:NO];
    switch (indexPath.section) {
//        case 0:
//            switch (indexPath.row)
//        {
//            case 0:
//                cell.textLabel.text = [BSEngine currentUser].nickname;
//                cell.className = @"SupplementaryInformationViewController";
//                break;
//            case 1:
//                cell.className = nil;
//                cell.textLabel.text = @"我的相册";
//                break;
//            case 2:
//                cell.className = @"CollectionViewController";
//                cell.textLabel.text = @"我的收藏";
//                [cell setBottomLine:YES];
//                break;
//            default:
//                break;
//        }
//            break;
                    case 0:
                        switch (indexPath.row) {
                            case 0:
                                cell.textLabel.text = @"修改密码";
                                cell.className = @"PasswordViewController";
                                break;
                            case 1:
                                cell.className = @"BindEmailViewController";
                                cell.textLabel.text = @"我的邮箱";
                                break;
//                            case 2:
//                                cell.className = @"BindPhoneViewController";
//                                cell.textLabel.text = @"绑定手机";
//                                [cell setBottomLine:YES];
//                                break;
                            default:
                                break;
                        }
                        break;
        case 1:
            switch (indexPath.row) {
                
                                    case 0:
                                        cell.className = @"NewMessageViewController";
                                        cell.textLabel.text = @"新消息通知";
                                        break;
                                    case 1:
                                        cell.className = @"PrivacyViewController";
                                        cell.textLabel.text = @"隐私";
                                        break;
                                    case 2:
                                        cell.className = @"BlackListViewController";
                                        cell.textLabel.text = @"黑名单";
                                        [cell setBottomLine:YES];
                                        break;
                default:
                    break;
            }
            
            break;
        case 2:
            switch (indexPath.row) {
                    //                case 0:
                    //                    cell.className = nil;
                    //                    cell.textLabel.text = @"检测更新";
                    //                    break;
                case 0:
                    cell.className = @"FeedbackViewController";
                    cell.textLabel.text = @"反馈意见";
                    break;
                case 1:
                    cell.className = @"HelpViewController";;
                    cell.textLabel.text = @"帮助中心";
                    break;
                case 2:
                    cell.textLabel.text = @"关于我们";
                    cell.className = @"AboutViewController";
                    [cell setBottomLine:YES];
                    break;
                default:
                    break;
            }
            
            break;
        default:
            break;
    }
    cell.backgroundColor =
    cell.contentView.backgroundColor = [UIColor whiteColor];
    [cell update:^(NSString *name) {
        cell.imageView.frame = CGRectMake(10, (cell.height - 35)/2, 35, 35);
        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
        cell.textLabel.left = 55;
        cell.textLabel.textColor = RGBCOLOR(50, 50, 50);
        cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
    }];
    return cell;
}

#pragma mark - UITableViewDataSource

- (UIView *)tableView:(UITableView *)sender viewForFooterInSection:(NSInteger)section {
    UIImageView *clearView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, sender.width, 15)];
    clearView.backgroundColor = [UIColor clearColor];
    return clearView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (void)tableView:(UITableView *)sender didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [sender deselectRowAtIndexPath:indexPath animated:YES];
    
    BaseTableViewCell *cell = (BaseTableViewCell*)[sender cellForRowAtIndexPath:indexPath];
    //    if (cell.tag==111) {
    //        Setting * vc=[[Setting alloc]init];
    //
    //        [self pushViewController:vc];
    //    }
    if (!cell.className) {
        
        
        self.updateManager = [[UpdateManager alloc] initCheckNow:YES del:self];
        
        User *user = [[BSEngine currentEngine] user];
        DLog(@"user:-----------%@",user);
        FriendPhotosViewController *con = [[FriendPhotosViewController alloc] initWithUser:user];
        [self pushViewController:con];
    } else {
        /*跳转到下一级页面**/
        Class class = NSClassFromString(cell.className);
        id tmpCon = [[class alloc] init];
        if ([tmpCon isKindOfClass:[UIViewController class]]) {
            UIViewController* con = (UIViewController*)tmpCon;
            [self pushViewController:con];
        }
    }
}

- (void)tableView:(UITableView *)sender willDisplayCell:(BaseTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section==0) {
        cell.imageView.image = [Globals getImageUserHeadDefault];
        [Globals imageDownload:^(UIImage * image) {
            if (image) {
                cell.imageView.image = image;
            }
        } url:[BSEngine currentUser].headsmall];
    }
    else{
        
        cell.imageView.image = LOADIMAGE(cell.textLabel.text);
    }
}


-(IBAction)JionCloudPlatform:(id)sender{
    GAWebViewController *web = [[GAWebViewController alloc] init];
    [web loadUrl:@"http://112.74.126.33:81/yunku/index.php"];
    web.title =@"云平台";
    [self pushViewController:web];
}

#pragma mark - CameraActionSheetDelegate
- (void)cameraActionSheet:(CameraActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        return;
    }
    if (buttonIndex == 0){
        [[AppDelegate instance] signOut];
    }
}

#pragma mark
#pragma mark - UpdateManagerDelegate
/*检查更新的回调**/
- (void)updateManagerDidCheck:(UpdateManager*)sender update:(BOOL)up {
    [sender showAlert];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
